package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.InjectFertilizerJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.BaseContextHandler;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevInjectFertilizer;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.dao.model.fertilizer.InjectFertilizer;
import cn.sciento.farm.devicemanager.dao.model.fertilizer.InjectFertilizerControl;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttBlockedSendManager;
import cn.sciento.farm.devicemanager.gateway.model.downlink.fertilizer.inject.InjectFertilizerControlPacket;
import cn.sciento.farm.devicemanager.service.InjectFertilizerService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.List;

import static cn.sciento.farm.devicemanager.common.util.ReturnUtil.getReturnMap;

@Service
@AutoJsonRpcServiceImpl
public class InjectFertilizerJsonApiImpl implements InjectFertilizerJsonApi {

    @Autowired
    private InjectFertilizerService injectFertilizerService;

    @Autowired
    private MqttBlockedSendManager mqttBlockedSendManager;

    @Override
    public ResponseMessage create(DevInjectFertilizer waterFertilizer) {
        if(StringUtils.isEmpty(waterFertilizer.getName())
        || StringUtils.isEmpty(waterFertilizer.getEui())
                || waterFertilizer.getAreaId() == null
        ){
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        if(waterFertilizer.getPipeAmount() == null || waterFertilizer.getPipeAmount() <1){
            waterFertilizer.setPipeAmount(3);
        }
        List<InjectFertilizer> fertilizerList = injectFertilizerService.getByAreaId(waterFertilizer.getAreaId());
        if(fertilizerList.size() >0){
            return ReturnUtil.getReturnMap(ResultConstant.SCREEN_MULTI_DEVICE);
        }
        waterFertilizer.setCreateTime(System.currentTimeMillis());
        waterFertilizer.setUpdateTime(System.currentTimeMillis());
        waterFertilizer.setEui(waterFertilizer.getEui().toLowerCase().trim());
        int count = injectFertilizerService.create(waterFertilizer);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage update(DevInjectFertilizer waterFertilizer) {
        if(StringUtils.isEmpty(waterFertilizer.getName())
                || StringUtils.isEmpty(waterFertilizer.getEui())
        ){
            return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
        }
        DevInjectFertilizer devInjectFertilizer = new DevInjectFertilizer();
        devInjectFertilizer.setId(waterFertilizer.getId());
        devInjectFertilizer.setName(waterFertilizer.getName());
        devInjectFertilizer.setRemark(waterFertilizer.getRemark());
        waterFertilizer.setUpdateTime(System.currentTimeMillis());
        int count = injectFertilizerService.update(devInjectFertilizer);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage get(Long id) {
        InjectFertilizer injectFertilizer = injectFertilizerService.getById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,injectFertilizer);
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = injectFertilizerService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage getByAreaId(Integer areaId) {
        List<InjectFertilizer> injectFertilizerList = injectFertilizerService.getByAreaId(areaId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,injectFertilizerList);
    }

    @Override
    public ResponseMessage control(InjectFertilizerControl control) {
        InjectFertilizer injectFertilizer = null;
        if(control.getId() == null){
            if(StringUtils.isNotEmpty(control.getEui())){
                injectFertilizer = injectFertilizerService.getByEui(control.getEui());
            }else {
                return ReturnUtil.getReturnMap(ResultConstant.INVALID_PARAMETER);
            }
        }else {
            injectFertilizer = injectFertilizerService.getById(control.getId());
        }
        InjectFertilizerControlPacket packet = new InjectFertilizerControlPacket();
        packet.setEui(injectFertilizer.getEui());
        packet.setActivate(control.getActivate() > 0);
        packet.setParam(control.getParam());
        packet.setMode(control.getMode());
        int count =  mqttBlockedSendManager.send(packet).getCode();
        return getReturnMap(ResultConstant.SUCCESS,count);
    }


}
